# -*- coding: utf-8 -*-
# @Time : 2022/1/4 17:06
# @Author : Limusen
# @File : element_excel_utils

import os
import xlrd3
from common.config_utils import local_config

#  元素识别信息读取工具类
current_path = os.path.dirname(__file__)
excel_path = os.path.join(current_path, '..', 'element_info_datas')


class ElementExcelUtils:

    def __init__(self, module_name, page_name, element_path=excel_path):
        """
        优化根据模块名读取excel内容
        :param module_name: 模块内容 在element_info_datas下的模块
        :param page_name:  登录页面 login_page1.py
        :param excel_path: 查找当前路径下
        """
        self.excel_path = os.path.join(element_path, module_name, page_name + '.xls')

        # 创建excel对象
        self.work_book = xlrd3.open_workbook(self.excel_path)
        # 创建表单对象
        self.sheet = self.work_book.sheet_by_index(0)
        # 获取总行数
        self.row_count = self.sheet.nrows

    def get_element_info(self):
        info_elements = {}
        for info in range(1, self.row_count):
            # 取出每行然后进行拼接处理
            element_info = {}
            element_info['element_name'] = self.sheet.cell_value(info, 1)
            element_info['locator_type'] = self.sheet.cell_value(info, 2)
            element_info['locator_value'] = self.sheet.cell_value(info, 3)
            timeout_value = self.sheet.cell_value(info, 4)
            # 如果超时时间为浮点型则直接使用timeout_value 如果为空则使用数字5
            element_info['timeout'] = timeout_value if isinstance(timeout_value, float) else local_config.time_out
            info_elements[self.sheet.cell_value(info, 0)] = element_info
        return info_elements


if __name__ == '__main__':
    ex = ElementExcelUtils("login", "login_page").get_element_info()
    print(ex)
    for i in ex.items():
        print(i)